<!--
 * @LastEditors: pmy
 * @LastEditTime: 2023-10-30 13:37:26
 * @FilePath: \bdcar-net\src\views\statisticalReportForm\mileageStatistics.vue
 * @Description: 里程统计 
-->
<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="车牌号" prop="numberPlate">
                <el-input v-model="queryParams.numberPlate" placeholder="请输入车牌号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="所属公司" prop="companyId">
                <el-tree-select v-model="queryParams.companyId" placeholder="请选择所属公司" clearable :data="treeList"
                    :expand-on-click-node="false" :render-after-expand="false" @change="handleQuery" check-strictly
                    :props="{ label: 'label' }" node-key="id" filterable />
            </el-form-item>
            <el-form-item label="统计时间" style="width: 308px;">
                <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
                    start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5" v-hasPermi="['statisticalReportForm:mileageStatistics:export']">
                <el-button type="warning" plain icon="Download" @click="handleExport">导出</el-button>
            </el-col>
            <el-col :span="1.5" v-hasPermi="['statisticalReportForm:mileageStatistics:summaryExport']">
                <el-popover placement="bottom" :width="320" trigger="click" :visible="visible" :persistent='false'>
                    <template #reference>
                        <el-button type="warning" plain icon="Download" @click="visible = true">汇总导出</el-button>
                    </template>
                    <el-form-item label="汇总时间">
                        <el-date-picker v-model="dateRange2" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
                            start-placeholder="开始日期" end-placeholder="结束日期" :clearable="false"></el-date-picker>
                    </el-form-item>
                    <div style="text-align: right;">
                        <el-button @click="visible = false">取消</el-button>
                        <el-button type="primary" @click="handleSummaryExport">确定</el-button>
                    </div>
                </el-popover>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table highlight-current-row v-loading="loading" :data="tableList">
            <template v-for="v in tableItems">
                <el-table-column :label="v.name" align="left" :prop="v.prop" v-if="v.type == 'time'">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row[v.prop], '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column :label="v.name" align="left" :prop="v.prop" v-else :width="v.width" />
            </template>
            <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleDetail(scope.row)"
                        v-hasPermi="['statisticalReportForm:mileageStatistics:detail']">明细</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>
</template>
<script setup lang="ts" name="MileageStatistics">
import dayjs from "dayjs"
import { useRouter } from 'vue-router';
import useAppStore from "../../store/modules/app";
const appStore = useAppStore()
const router = useRouter();
import { reactive, toRefs, ref, getCurrentInstance, onDeactivated } from "vue"
import { getSearchCompanyListRole } from "../../api/system/dept";
import request from '../../utils/request'
const loading = ref(true);
const visible = ref(false);
const tableList = ref([]);
const treeList = ref([]);
const dateRange = ref([dayjs(new Date()).add(-1, "days").format("YYYY-MM-DD"), dayjs(new Date()).format("YYYY-MM-DD")]);
const dateRange2 = ref([dayjs(new Date()).add(-1, "days").format("YYYY-MM-DD"), dayjs(new Date()).format("YYYY-MM-DD")]);
const { proxy } = getCurrentInstance();
//@ts-ignore
const parseTime = proxy.parseTime;
const tableItems = ref([
    { name: '车牌号', prop: "numberPlate" },
    { name: '设备号', prop: "dvo" },
    { name: '所属公司', prop: "companyName", width: 500 },
    { name: '行驶里程(km)', prop: "kilometer" },
    { name: '行驶时长(min)', prop: "driverMinutes" },
    { name: '统计日期', prop: "day", type: 'time' },
]);
const total = ref(0);
const showSearch = ref(true);
const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 15,
        numberPlate: null,
        companyId: null,
    },
});
getSearchCompanyListRole().then(res => {
    treeList.value = res.data
})
const { queryParams } = toRefs(data);
onDeactivated(() => {
    visible.value = false;
})

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    //@ts-ignore
    proxy.resetForm("queryRef");
    dateRange.value = [dayjs(new Date()).add(-1, "days").format("YYYY-MM-DD"), dayjs(new Date()).format("YYYY-MM-DD")];
    handleQuery();
}
/** 查询列表 */
function getList() {
    loading.value = true;
    request({
        url: '/comlbs/reportStatistics/deviceMileage/list',
        method: 'post',
        //@ts-ignore
        params: proxy.addDateRange2(queryParams.value, dateRange.value)
    }).then(({ data }: any) => {
        tableList.value = data.list;
        total.value = data.total;
        loading.value = false;
    }).catch(err => {
      loading.value = false;
    });
}

/** 导出按钮操作 */
function handleExport() {
    //@ts-ignore
    proxy.download('comlbs/reportStatistics/deviceMileage/listExport',
        //@ts-ignore
        proxy.addDateRange2(queryParams.value, dateRange.value)
        , `里程统计_${dayjs(new Date()).format("YYYY-MM-DD_HH:mm:ss")}.xlsx`)
}

/** 汇总导出按钮操作 */
function handleSummaryExport() {
    visible.value = false;
    //@ts-ignore
    proxy.download('comlbs/reportStatistics/deviceMileage/listSummaryExport',
        //@ts-ignore
        proxy.addDateRange2(queryParams.value, dateRange2.value)
        , `里程汇总统计_${dayjs(new Date()).format("YYYY-MM-DD_HH:mm:ss")}.xlsx`)
}



/** 明细按钮操作-跳转到里程明细 */
function handleDetail(obj: any) {
    let time = obj.day.slice(0, 10);
    appStore.setPageQuery({
        dateRange: [time + ' 00:00:00', time + ' 23:59:59'],
        numberPlate: obj.numberPlate,
        companyId: obj.companyId
    });
    router.push({ path: '/statisticalReports/mileageDetail' })
}
getList();
</script>
<style lang='less' scoped></style>